<?php
/***
 * 颜色后台控制器
 *
 *
 * @author    lanhongjie <lanhj@yunys.com.cn>
 * @copyright 2016 QuanZhouYunYiShang Inc.
 */

namespace App\Http\Controllers\Backstage;

use App\Http\Controllers\Controller;
use App\Services\ImgServices\ImgTool;
use App\Services\Tool\GeneralTool;
use Illuminate\Http\Request;
use App\Services\ColourService\BackstageColourService as ColourService;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Response;

class ColourController extends Controller
{

    protected $colourService;
    protected $generalTool;
    protected $imgTool;

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct(ColourService $colourService,GeneralTool $generalTool,ImgTool $imgTool)
    {
        $this->colourService = $colourService;
        $this->generalTool = $generalTool;
        $this->imgTool = $imgTool;
    }


    public function getIndex()
    {
        return redirect('backstage/colour/colour-list');
    }

    /**
     * 获取颜色列表
     * @param Request $request
     * @return $this
     */
    public function getColourList(Request $request)
    {
        $colours =  $this->colourService->getColourList($request);
        return view('backstage.colour.colourList')->with('colours',$colours);
    }

    /**
     * 获取颜色列表
     * @param Request $request
     * @return mixed
     */
    public function postJsonColourList(Request $request){
        $colours =  $this->colourService->getColourList($request);
        return response()->json($colours);
    }

    /**
     * 获取所有颜色列表
     * @param Request $request
     * @return mixed
     */
    public function getAllColourList(Request $request){
        $crowds =  $this->colourService->getAllColourList($request);
        if(empty($crowds)){
            $result["state"] = -1;
            $result["message"] = "失败";
            $result["data"] = array();
        }else{
            $result["state"] = 1;
            $result["message"] = "成功";
            $result["data"] = $crowds;
        }
        return response()->json($result);
    }
    /**
     * 添加颜色
     * @param Request $request
     * @return \Illuminate\View\View
     */
    public function getAddColour(Request $request){
        return view('backstage.colour.addColour');
    }

    /**
     * 修改颜色
     * @param Request $request
     * @return \Illuminate\View\View
     */
    public function getUpdateColour(Request $request){
        $colour_id = $request->input("colour_id");
        $colour =  $this->colourService->getColourForId($colour_id);
        if($colour==null)
        {
            return redirect()->back()->with('error_message', '颜色不存在');
        }
        return view('backstage.colour.updateColour')->with('colour',$colour);
    }

    /**
     * 修改颜色
     * @param Request $request
     * @return $this|\Illuminate\Http\RedirectResponse
     */
    public function postUpdateColour(Request $request)
    {
        $colour_id = intval($request->input("colour_id"));
        $colour =  $this->colourService->getColourForId($colour_id);
        if($colour==null)
        {
            return redirect()->back()->with('error_message', '颜色不存在');
        }else{
            $name = $request->input('colour_name');
            $color_code = $request->input('colour_code');
            $image = $request->file('colour_image');
            $description = $request->input('description');
            $new_colour =  array('colour_name'=>$name,'colour_code'=>$color_code,'colour_image'=>$image,'description'=>$description);
            $validator =  $this->colourService->updateValidator($new_colour);
            $validator->after(function($validator) use ($colour_id,$name,$image,$color_code)
            {
                if($image==null&&$color_code=="")
                {
                    $validator->errors()->add('color_code', '未上传色块色值必填!');
                }
                //判断除当前颜色名称外有没有重复颜色名称
                $b =  $this->colourService->getColourForNameIsNotCurrent($colour_id,$name);
                if($b!=null)
                {
                    $validator->errors()->add('name', '颜色名称已存在!');
                }
            });
            if ($validator->fails())
            {
                $request->flash();
                return redirect()->back()->withErrors($validator->errors())->withInput();;
            }
            if($image!=null)
            {
                //图片保存
                $imageUrl = $this->imgTool->imgSave($image,2);
                $new_colour['colour_image'] = $imageUrl;
            }else{
                $imageUrl = $this->imgTool->saveColorBlock($color_code);
                $new_colour['colour_image'] = $imageUrl;
            }
            $new_colour['id'] = $colour_id;
            $num = $this->colourService->updateColour($new_colour);
            if($num>=1)
            {
                return redirect('backstage/colour/colour-list')->with('success_message', '修改成功');
            }else{
                return redirect()->back()->with('error_message', '未修改如何数据');
            }
        }

    }
    /**
     * 保存
     * @param Request $request
     * @return $this|\Illuminate\Http\RedirectResponse
     */
    public function postAddColour(Request $request)
    {

        $name = $request->input('colour_name');
        $color_code = $request->input('colour_code');
        $image = $request->file('colour_image');
        $description = $request->input('description');
        $colour =  array('colour_name'=>$name,'colour_code'=>$color_code,'colour_image'=>$image,'description'=>$description);
        $validator =  $this->colourService->addValidator($colour);
        $validator->after(function($validator) use ($name,$image,$color_code)
        {
            if($image==null&&$color_code=="")
            {
                $validator->errors()->add('colour_code', '未上传色块色值必填!');
            }
            //判断除当前颜色名称外有没有重复颜色名称
            $b =  $this->colourService->getColourForNameIsNotCurrent(0,$name);
            if($b!=null)
            {
                $validator->errors()->add('name', '颜色名称已存在!');
            }
        });
        if ($validator->fails())
        {
            $request->flash();
            return redirect()->back()->withErrors($validator->errors())->withInput();;
        }
        if(!empty($image))
        {
            $imageUrl = $this->imgTool->imgSave($image,2);
            $colour['colour_image'] = $imageUrl;
        }else{
            $imageUrl = $this->imgTool->saveColorBlock($color_code);
            $colour['colour_image'] = $imageUrl;
        }
         $this->colourService->addColour($colour);
         return redirect()->back()->with('success_message','添加成功');
    }

    /**
     * 删除颜色
     * @param Request $request
     */
    public function postDeleteColour(Request $request)
    {
        $colour_id = $request->input("colour_id");
        $num =  $this->colourService->deleteColour($colour_id);
        return $num;
    }

    /**
     * 重新生成色块
     * @param Request $request
     * @return int
     */
    public function postReloadColorCode(Request $request)
    {
        $colour_id = $request->input("colour_id");
        $colour =  $this->colourService->getColourForId($colour_id);
        if($colour!=null)
        {
            $imagePath = $this->imgTool->saveColorBlock($colour->colour_code);
            $colour->colour_image = $imagePath;
            $num =  $this->colourService->updateColour(['colour_image'=>$imagePath,'id'=>$colour_id]);
            $imageUrl=empty($imagePath)?'':ImgTool::getImgUrlByImgPath($imagePath);
            return $imageUrl;
        }else{
            return 0;
        }
    }
}
